import {Injectable} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {Observable} from 'rxjs/Observable';

@Injectable()
export class NavigationService {

    url_navigationlist = 'navigation/treelist';
    url_navigationadd = 'navigation/save';
    url_navigationshow = 'navigation/show/';
    url_navigationdel = 'navigation/del/';
    url_navigationByType = 'navigation/type/';

    constructor(private http: _HttpClient) {
    }

    /**
     * 得到菜单列表
     * @returns {Observable<{}>}
     */
    getNavigationList(): Observable<{}> {
        return this.http.get(this.url_navigationlist);
    }

    /**
     * 获取菜单类型
     * @param {string} type
     * @returns {Observable<{}>}
     */
    getNavigationListByType(type: string): Observable<{}> {
        return this.http.get(this.url_navigationByType + type);
    }

    /**
     * 根据父类ID获取列表
     * @param {string} type
     * @param {number} id
     * @returns {Observable<{}>}
     */
    getNavigationListByParentId(type: string, id: number): Observable<{}> {
        return this.http.get(this.url_navigationByType + type + '/' + id);
    }

    /**
     * 显示菜单详情
     * @param {string} id
     * @returns {Observable<{}>}
     */
    showNavigation(id: string): Observable<{}> {
        return this.http.get(this.url_navigationshow + id);
    }

    /**
     * 添加/修改菜单
     * @param i
     * @returns {Observable<string>}
     */
    addNavigation(i): Observable<string> {
        const xx = {
            href: i.href,
            content: i.content,
            icon: i.icon,
            id: i.id ? i.id : 0,
            parentId: i.parentId ? i.parentId : 0,
            sort: i.sort ? i.sort : 0,
            type: i.type ? i.type : 0,
            title: i.title,
        };
        return this.http.post<string>(this.url_navigationadd, JSON.stringify(xx));
    }

    /**
     * 删除菜单
     */
    delNavigation(id: string): Observable<{}> {
        return this.http.delete(this.url_navigationdel + id);
    }

}
